#encoding=utf-8
"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".

Replace this with more appropriate tests for your application.
"""
import webconfig
from django.test import TestCase
import MySQLdb
from websys.models import LogRecharge,UserInfo,LogLogin,LogOnline
import fun,requests
class SimpleTest(TestCase):
    def test_basic_addition(self):
        """
        Tests that 1 + 1 always equals 2.
        """
        self.assertEqual(1 + 1, 2)


# def insert_data_recharge():
#     try:
#         conn = MySQLdb.connect(host=webconfig.con_db['host'], port=webconfig.con_db['port'],
#                                user=webconfig.con_db['user'], passwd=webconfig.con_db['password'],
#                                db=webconfig.con_db['database'])
#         cursor = conn.cursor()
#
#         sql = "select * from log_recharge"
#         cursor.execute(sql)
#         data = cursor.fetchall()
#         for i in data:
#             if not LogRecharge.objects.filter(order_id=i[11]).exists():
#                 recharge_info=LogRecharge()
#                 recharge_info.uid = i[3]
#                 recharge_info.server=i[10]
#                 recharge_info.platform='guozi'
#                 recharge_info.order_id=i[11]
#                 recharge_info.orderid=i[11]
#                 recharge_info.item=i[8]
#                 recharge_info.money=i[6]
#                 recharge_info.isPay=1
#                 recharge_info.account=i[4]
#                 recharge_info.create_time=fun.StampToTime(i[5])
#                 recharge_info.save()
#         return 'success'
#
#     except:
#         return 'fail'
#
#
#
# def insert_data_user():
#     #try:
#     conn = MySQLdb.connect(host=webconfig.con_db['host'], port=webconfig.con_db['port'],
#                            user=webconfig.con_db['user'], passwd=webconfig.con_db['password'],
#                            db='jx_user')
#     cursor = conn.cursor()
#
#     sql = "select * from t_player where f_playerid=26235561711553"
#     cursor.execute(sql)
#     data = cursor.fetchall()
#     for i in data:
#         if not UserInfo.objects.filter(uid=i[0]).exists():
#             user_info=UserInfo()
#             user_info.uid=i[0]
#             user_info.account=i[4]
#             user_info.username=i[8]
#             user_info.server=i[2]
#             user_info.platform='guozi'
#             user_info.create_time=fun.StampToTime(i[5])
#             user_info.save()
#         # else:
#         #     user=UserInfo.objects.get(uid=i[0])
#         #     if user.server=='s1':
#         #         user.server='s2'
#         #         user.save()
#
#     return 'success'
#
#     # except:
#     #     return 'fail'
# #print insert_data_recharge()
# print insert_data_user()
# from fun import StampToTime
#


# print StampToTime(1508994670)
# from websys.api import ProtocolHelper
#
# a = 'jEiLCJjcmVhdGVkIjoxNTA4MjMwODQ2LjM5ODk5OTkyOTR9XSwib3JkZXIiOiJhOWU0ZDRlZWIzMTkxMWU3YTk4ZjI0ZmQ1MjhjOGNiZCJ9eyJ1aWQiOjEsImFyZWEiOjEsInBpZCI6IlRFU1QiLCJyZWFzb24iOiJVX0ZMT0dJTiIsImdpZCI6ImNyIiwiZGF0YSI6W3siaXAiOiIxMjcuMC4wL'
# print ProtocolHelper.Decode_OSS(a,'oss')
# a ='{%22uid%22:%2226212594491137%22,%22area%22:%22s1%22,%22pid%22:%22guozi%22,%22reason%22:%22B_FRAG%22,%22gid%22:%22cr%22,%22data%22:%22[{\%22created\%22:1509084674.237}]%22,%22order%22:%22262125944911371509084674237%22}'
# import urllib,ujson
# b ="{'uid':15857585,'nickname':'昵称','content':'测试','get_time':'2017-11-02 15:05:47'}"
#
# c =ujson.dumps(b)
# print c

#礼物码提取
# def testselect():
#     conn = MySQLdb.connect(host='127.0.0.1', port=3306,user='root', passwd='hagoot3008#))*',db='gm_manager')
#
#     cursor = conn.cursor()
#
#     sqls="select * from websys_gift_code where (activity_id=3 or activity_id=4 or activity_id=5) and create_time>'2017-11-06 00:00:00'"
#     cursor.execute(sqls)
#     data = cursor.fetchall()
#     print len(data)
#     for i in data:
#         f=open("data.txt", "a+")
#         f.write('活动ID：%s ,礼物码：%s'%(i[7],i[9])+'\n')
#         f.close()
#     #print data
#
# testselect()


# from websys.models import Gift_Code
# def insert_giftcode():
#     try:
#         gift_info = Gift_Code.objects.filter(activity_id__in=(3,5),create_time__range=('2017-11-06 00:00:00','2017-11-06 23:59:00'))
#
#         conn = MySQLdb.connect(host=webconfig.con_db['host'], port=webconfig.con_db['port'],
#                                         user=webconfig.con_db['user'], passwd=webconfig.con_db['password'],
#                                         db=webconfig.con_db['database'])
#         cursor = conn.cursor()
#         for i in gift_info:
#             sql = "INSERT INTO t_gift_code (f_serial_no, f_is_plat, f_origin, f_expired_utc, f_items, f_gift_type, f_activity_id, f_gift_num, f_code) "
#             sql += "VALUES ( %s, 1,'', %d, '%s', %d, %d,1, '%s');" % \
#                    (str(i.serial_no), int(fun.TimeToStamp(i.expired_utc)), str(i.items), 0, int(i.activity_id), str(i.code))
#             cursor.execute(sql)
#         conn.commit()
#         conn.close()
#         return 'success'
#     except:
#          return 'fail'
#
# print insert_giftcode()


# def copydata():
#     # try:
#
#     conn = MySQLdb.connect(host='127.0.0.1', port=3306, user='root', passwd='hagoot3008#))*', db='jx_user1')
#     lists=[26114640996369]
#     for i in lists:
#         cursor = conn.cursor()
#         sqls = "select * from t_player_hero where f_playerid=%d"%i
#         cursor.execute(sqls)
#         data = cursor.fetchall()
#         for j in data:
#             sql ="insert into t_player_hero(f_playerid, f_uid, f_heroid, f_level, f_location, f_advanced_level,f_tactic_cost_count, f_tactic_level, f_tactic_exp) values (%d,%d,%d,%d,%d,%d,NULL ,NULL,NULL)" % (i+1,j[1],j[2],j[3],j[4],j[5])
#             cursor.execute(sql)
#
#             # sql2 = "insert into t_player_hero(f_playerid, f_uid, f_heroid, f_level, f_location, f_advanced_level,f_tactic_cost_count, f_tactic_level, f_tactic_exp) values (%d,%d,%d,%d,%d,%d,NULL,NULL,NULL)" % (
#             # j[0] + 2000, j[1], j[2], j[3], j[4], j[5])
#             # cursor.execute(sql2)
#         cursor = conn.cursor()
#         sql3="select * from t_player_hero_piece where f_playerid=%d"%i
#         cursor.execute(sql3)
#         datas = cursor.fetchall()
#         for h in datas:
#             sql4="insert into t_player_hero_piece(f_playerid,f_heroid,f_count) VALUES (%d,%d,%d)"%(h[0]+1,h[1],h[2])
#             cursor.execute(sql4)
#
#             # sql5 = "insert into t_player_hero_piece(f_playerid,f_heroid,f_count) VALUES (%d,%d,%d)" % (
#             # h[0] + 2000, h[1], h[2])
#             # cursor.execute(sql5)
#
#         cursor = conn.cursor()
#         sql6 ="select * from t_player_equipment where f_playerid=%d"%i
#         cursor.execute(sql6)
#         datass = cursor.fetchall()
#
#         for e in datass:
#             sql7="insert into t_player_equipment (f_playerid,f_uid,f_equipment_id,f_location,f_level,f_refine_level,f_forge_level) VALUES (%d,%d,%d,%d,%d,%d,%d)"%(e[0]+1,e[1],e[2],e[3],e[4],e[5],e[6])
#             cursor.execute(sql7)
#
#             # sql8 = "insert into t_player_equipment (f_playerid,f_uid,f_equipment_id,f_location,f_level,f_refine_level,f_forge_level) VALUES (%d,%d,%d,%d,%d,%d,%d)" % (
#             # e[0] + 2000, e[1], e[2], e[3], e[4], e[5], e[6])
#             # cursor.execute(sql8)
#
#         cursor = conn.cursor()
#         sql9 = "select * from t_player_equip_piece where f_playerid=%d" % i
#         cursor.execute(sql9)
#         datasss = cursor.fetchall()
#         for hs in datasss:
#             sql10 = "insert into t_player_equip_piece(f_playerid,f_equipid,f_count) VALUES (%d,%d,%d)" % (
#             hs[0] + 1, hs[1], hs[2])
#             cursor.execute(sql10)
#
#             # sql11 = "insert into t_player_equip_piece(f_playerid,f_equipid,f_count) VALUES (%d,%d,%d)" % (
#             #     hs[0] + 2000, hs[1], hs[2])
#             # cursor.execute(sql11)
#
#     conn.commit()
#     conn.close()
#     return 'success'
#
#     # except:
#     #     return 'fail'
#
#
# print copydata()

# from websys.models import Report_Data
# import datetime
# data = Report_Data.objects.filter(create_time__range=('2017-11-30 12:20:00','2017-11-30 12:40:00')).values('create_time').distinct()
# for i in data:
#     times =datetime.datetime.strftime(i['create_time'],'%Y-%m-%d %H:%M:00')
#     print times
# print len(data),data


# #删除每个db 的道具
#
# def dele_item():
#     try:
#         for i in range(1,22):
#             if i==1:
#                 conn = MySQLdb.connect(host=webconfig.con_db['host'], port=webconfig.con_db['port'],
#                                            user=webconfig.con_db['user'], passwd=webconfig.con_db['password'],
#                                            db='jx_user')
#             else:
#                 conn = MySQLdb.connect(host=webconfig.con_db['host'], port=webconfig.con_db['port'],
#                                        user=webconfig.con_db['user'], passwd=webconfig.con_db['password'],
#                                        db='jx_user%d'%i)
#             cursor = conn.cursor()
#             sql="DELETE FROM t_player_bag_item where f_itemid IN (7012,7011,7022,7021)"
#             cursor.execute(sql)
#             conn.commit()
#             conn.close()
#             print 'success--->>%d'%i
#         return 1
#
#     except:
#         print 'fail'
#         return 0
#
#
#
# print dele_item()
#
# import datetime
#
# #根据需求删除 xx 分钟内的重复数据
# from websys.models import LogOnline
#
# def delete_repeat_Data():
#     all_data=LogOnline.objects.all().order_by('-create_time')
#     delta = datetime.timedelta(seconds=29)
#     for each_data in all_data:
#         repeat_data=LogOnline.objects.filter(uid=each_data.uid,server=each_data.server,create_time__gt=each_data.create_time-delta,create_time__lt=each_data.create_time+delta)
#         lenths=len(repeat_data)
#         n=0
#         for i in repeat_data:
#             if n<lenths-1:
#                 i.delete()
#                 n+=1
#         print '----please----'
#
#         #print type(each_data.create_time),each_data.create_time,'----------',each_data.create_time+delta
#
#
# delete_repeat_Data()